package com.sgb.kjwl.view.ui.activity.self.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMangerMoreBinding;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.view.ui.adapter.pick.MangermoreAdapter;
import com.swgk.core.customview.ItemDragHelperCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class MangermoreActivity extends AppActivity  {
    private ActivityMangerMoreBinding mBinding;
    private List<PermissionMenuEntity> mlist=new ArrayList<>();
    private List<PermissionMenuEntity> newmlist=new ArrayList<>();
    private MangermoreAdapter adapter;
    MangermoreModel mangermoreModel;
    private PermissionMenuEntity permissionMenuEntity;
    @Override
    protected void initView() {
         mlist = (List<PermissionMenuEntity>) getIntent().getSerializableExtra("list");
         if (mlist.size()>0){
             permissionMenuEntity =mlist.get(0);
             mlist.remove(0);
         }

        mangermoreModel =new ViewModelProvider(this).get(MangermoreModel.class);
        mBinding = DataBindingUtil.setContentView(MangermoreActivity.this, R.layout.activity_manger_more);
        mBinding.finish.setOnClickListener(this);
        mBinding.reture.setOnClickListener(this);
        mBinding.include.ivBack.setOnClickListener(this);
        GridLayoutManager manager = new GridLayoutManager(MangermoreActivity.this, 4){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        mBinding.rlvList.setLayoutManager(manager);
        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mBinding.rlvList);
        adapter = new MangermoreAdapter(MangermoreActivity.this, helper, mlist, mlist);

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                return viewType == MangermoreAdapter.TYPE_MY || viewType == MangermoreAdapter.TYPE_OTHER ? 1 : 4;
            }
        });
        mBinding.rlvList.setAdapter(adapter);
        adapter.setOnMyChannelItemClickListener(new MangermoreAdapter.OnMyChannelItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Toast.makeText(MangermoreActivity.this, mlist.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        adapter.setOnFinishClick(new MangermoreAdapter.OnFinishClick() {
            @Override
            public void onFinish(List<PermissionMenuEntity> editList) {
            }
        });

    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.finish:
               newmlist = adapter.getlist( mBinding.rlvList);
               newmlist.add(0,permissionMenuEntity);
               mangermoreModel.updateMenuOrder(MangermoreActivity.this,newmlist);
               break;
           case R.id.reture:
               mangermoreModel.restoreDefault(MangermoreActivity.this);
               break;
           case R.id.iv_back:
               finish();
               break;
       }
    }
    public static void startMangermoreactivity(Context context,  List<PermissionMenuEntity> dataList){
        Intent intent =new Intent(context,MangermoreActivity.class);
        intent.putExtra("list", (Serializable) dataList);
        context.startActivity(intent);

    }
}
